package com.zxk.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxk.api.dao.NewsDao;
import com.zxk.api.service.NewsService;
import com.zxk.core.common.constant.NewsConstant;
import com.zxk.core.common.model.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author zxk
 * @description 新闻业务层
 * @date 2020/8/28
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsDao,News> implements NewsService {
    @Autowired
    private NewsDao newsDao;


    @Override
    @Transactional
    public void saveNews(News news) {
        //保存之前查看是否有重复数据
        News searchNew = new News();
        searchNew.setDetailLink(news.getDetailLink());

        List<News> newsList = find(1,searchNew).getRecords();
        if(newsList.size() == 0) {
            newsDao.insert(news);
        }

    }

    @Override
    public IPage<News> find(Integer pageNum, News news) {
        Page<News> page = new Page<>(pageNum, Integer.parseInt(NewsConstant.PAGEINFO.getVal()));
        Wrapper<News> wrapper = new  QueryWrapper(news);
        IPage<News> newsIPage = newsDao.selectPage(page, wrapper);
        return newsIPage;
    }

    @Override
    public List<News> findNewsByTime(Date date) {
//        https://blog.csdn.net/long476964/article/details/79677526
        News news = new News();
        news.setCreateTime(date);
//        List<News> list = this.newsDao.selectNewsByTime(date);
        return null;
    }

    @Override
    public List<News> findAll() {
        List<News> list = this.newsDao.selectList(null);
        return list;
    }

    @Override
    public IPage<News> findByLike(String title, Integer pageNum) {
        QueryWrapper<News> wrapper = new QueryWrapper();
        wrapper.like("title",title);
        Page<News> page = new Page<>(pageNum, Integer.parseInt(NewsConstant.PAGEINFO.getVal()));
        return newsDao.selectPage(page,wrapper);
    }

    @Override
    public IPage<News> findByTime(String time, Integer pageNum) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Page<News> page = new Page<>(pageNum, Integer.parseInt(NewsConstant.PAGEINFO.getVal()));
        QueryWrapper<News> wrapper = new QueryWrapper();
        try {
            Date createTime = sdf.parse(time);
            wrapper.lt("create_time",createTime);
            return newsDao.selectPage(page,wrapper);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void deleteById(Long id) {
        newsDao.deleteById(id);
    }
}
